package io.renren.modules.webofthing.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.webofthing.entity.IotUserEntity;
import io.renren.modules.webofthing.service.IotUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * @author jlk
 * @email sunlightcs@gmail.com
 * @date 2021-11-17 13:59:17
 */
@RestController
@RequestMapping("/iotuser")
public class IotUserController {
    @Autowired
    private IotUserService iotUserService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = iotUserService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        IotUserEntity iotUser = iotUserService.getById(id);

        return R.ok().put("iotUser", iotUser);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody IotUserEntity iotUser) {
        boolean flag = iotUserService.saveIotUserEntity(iotUser);
        if (flag) {
            return R.ok("添加用户成功");
        }
        return R.error("添加用户失败");
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody IotUserEntity iotUser) {
        iotUserService.updateById(iotUser);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        iotUserService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
